<?php
class File_selectorAttr extends BaseAttr {
	public function File_selectorAttr($attr, $lang=null, $mutil_lang=false){
		parent::BaseAttr($attr, $lang, $mutil_lang);
	}
	
	public function get_post_val(&$getted_post_data){
		$trait = $this->attr['trait'];
		$fetch_method = 'fetch_data_' . $trait;
		$this->$fetch_method($getted_post_data);
	}
	

	/**
	 * 模板选择器获取数据
	 * @param array $getted_post_data
	 */
	private function fetch_data_template(&$getted_post_data){
		$id = $this->attr['id'];
		
		$getted_post_data[$id] = $_POST[$id];
	}
	
	/**
	 * 目录选择器获取数据(专门为分类静态文件存放目录而设定)
	 * @param array $getted_post_data
	 */
	private function fetch_data_dir(&$getted_post_data){
		$id = $this->attr['id'];
		if(!isset($_POST['parent'])){
			//没有设置有依赖的两项
			$this->error = '本表单依赖不存在：parent！';
			return;
		}
		if($this->mutil_lang!==false){
			//多语言
			if(!isset($getted_post_data[$this->mutil_lang]['name'])){
				$this->error = '本表单依赖不存在：name！';
				return;
			}
			$name = $getted_post_data[$this->mutil_lang]['name'];
		}else{
			if(!isset($getted_post_data['name'])){
				$this->error = '本表单依赖不存在：name！';
				return;
			}
			$name = $getted_post_data['name'];
		}
		$model = $this->formHelper->model_name;
		if(strpos($model,'_cate')<=-1){
			//如果不包含_cate的，则自行添加
			$model .= '_cate';
		}
		
// 		$name = $getted_post_data[LANGUAGE]['name'];
		$parent = $getted_post_data['parent'];
		$cate_mode = m($model);
		if(isset($_POST['base_path_' . $id])){
			//新建的分类
			$base_path = $_POST['base_path_' . $id];
			if(isset($_POST['pinyin_' . $id])){
				//使用拼音
				$path = Pinyin::cn2pinyin($name);
			}else{
				$path = $_POST['path_' . $id];
				if(empty($path)){
					//如果没有设置path，则使用拼音作为它的路径
					$path = Pinyin::cn2pinyin($name);
				}
			}
			if($base_path == 'site_base'){
				//如果是基于网站
				$path = '/' . $path;
			}else{
				//基于上级目录
				if($parent === '0'){
					//顶级目录
					$path = '/' . $path;
				}else{
					//如果不是顶级目录，则获取目录
					$condition = array('site_id'=>SITE_ID, '_id'=>$parent);
					$parent_cate = $cate_mode->get_by_attrs($condition, 'path');
					if(empty($parent_cate)){
						//如果不空
						$this->error = '父级分类不存在！';
						return;
					}
					$path = $parent_cate['path'] . '/' . $path;
				}
			}
		}else{
			//直接取
			$path = $_POST['path_' . $id];//$this->get_post_val('path');
		}
		$getted_post_data[$id] = $path;
	}
}

?>